package com.zyy.smartcity1129.zhdj;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.DjDtInfo;
import com.zyy.smartcity1129.data.PLInfo;
import com.zyy.smartcity1129.home.HomeFragment;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName: DJDynamic1Fragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 12/12/2021$
 * @Version: 1.0
 */
public class DJDynamic1Fragment extends BaseFragment {
    private ImageView ig;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private EditText edit;
    private Button btn;
    private RecyclerView rv;
    private DjDtInfo info = DJDynamicFragment.info;

    @Override
    protected int layId() {
        return R.layout.f_djdt1;
    }

    @Override
    protected String title() {
        return "详细";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    RvAd<PLInfo> rvAd;
    LinkedList<PLInfo> list = new LinkedList<>();

    @Override
    protected void init(View iv) {
        initView(iv);
        Glide.with(ig.getContext()).load(info.getImg()).into(ig);
        tv1.setText(info.getTitle());
        tv2.setText(info.getTime());
        tv3.setText(info.getContext());
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        initData();
        initAdapter(list);
        rv.setAdapter(rvAd);
        btn.setOnClickListener(view -> {
            String sEdit = edit.getText().toString().trim();
            if (!sEdit.equals("")) {
                list.addFirst(new PLInfo(HomeFragment.userBeans.getNickName(), sEdit));
                initAdapter(list);
                rv.setAdapter(rvAd);
                edit.setText("");
                Toast.makeText(getContext(), "发送成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getContext(), "内容不可为空", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData() {
        list.add(new PLInfo("iSongZY", "学起来！！！"));
        list.add(new PLInfo("蜗得天", "以史为鉴，开创未来！"));
        list.add(new PLInfo("蜜汁跳舞", "马上记下[拳头][拳头][拳头][拳头]"));
        list.add(new PLInfo("柳州政法", "多么幸运我们拥有智慧，果敢，与稳重的掌舵人与先锋们 – 机不可失时不再来。感激，自豪有自强不息的同胞，谦逊、和平，勤奋劳作的每一个中国劳动者，生产者，创造者。也感激这精彩纷呈的广阔世界–你精彩了中国，中国丰富了你。"));
    }

    private void initAdapter(List<PLInfo> list) {
        rvAd = new RvAd<PLInfo>(list, R.layout.rv_pl) {
            private void initView(View vi) {
                tvNewsPL1 = (TextView) vi.findViewById(R.id.tvNewsPL1);
                tvNewsPL2 = (TextView) vi.findViewById(R.id.tvNewsPL2);
            }

            private TextView tvNewsPL2;
            private TextView tvNewsPL1;

            @Override
            protected void initR(View vi, PLInfo plInfo, int p) {
                initView(vi);
                tvNewsPL1.setText(plInfo.getName());
                tvNewsPL2.setText(plInfo.getContext());
            }
        };
    }

    private void initView(View iv) {
        ig = (ImageView) iv.findViewById(R.id.ig);
        tv1 = (TextView) iv.findViewById(R.id.tv1);
        tv2 = (TextView) iv.findViewById(R.id.tv2);
        tv3 = (TextView) iv.findViewById(R.id.tv3);
        edit = (EditText) iv.findViewById(R.id.edit);
        btn = (Button) iv.findViewById(R.id.btn);
        rv = (RecyclerView) iv.findViewById(R.id.rv);
    }
}
